package controller.menubar.show;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;

import javax.swing.JOptionPane;

import model.GlobalParams;

import view.MainFrame;

public class CheckSumVerify implements ActionListener {
	
	public CheckSumVerify(MainFrame frame) {
		
		this.frame = frame;
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {			
	
		File f =  frame.getMainPanel().getPrimary().getDiskContent().getSelectedFile();		
		FileInputStream is = null;
		
		try {
			is = new FileInputStream(f);
		} catch (FileNotFoundException e) {			
			e.printStackTrace();
		}
		
		CheckedInputStream  s = new CheckedInputStream(is, new CRC32());		
		BufferedInputStream bf = new BufferedInputStream(s);
		
		try {
			while (bf.read() != -1) {}
		} catch (IOException e) {			
			e.printStackTrace();
		}
		
		JOptionPane.showConfirmDialog(frame, s.getChecksum().getValue(), GlobalParams.getInstance().stringCollection.getOther_operation_manager(), JOptionPane.WARNING_MESSAGE);
        
		
	}
	
	private MainFrame frame;

}
